package iosu.java.games.razer.server;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class RazerServerGUI extends JFrame implements 
		WindowListener {
	JFrame frame;
	RazerServer server;
	// boton de shutdown del servidor
	private JButton shutdownButton;
	private JLabel partidas, clientes;
	private JTable tablaPartidas;
	private JScrollPane scrollPane;
	private DefaultTableModel tablemodel;
	int numPartidasPrevio = 0;
	private ArrayList<Integer> partidasentabla = new ArrayList<Integer>();
		
	public RazerServerGUI(RazerServer s) {
		this.server = s;
		// ajusto el tamanyo
		this.setSize(640, 480);
		this.createAndShowGUI();
	}

	private void createAndShowGUI() {
		// in the NorthPanel the PortNumber the Start and Stop buttons
		JPanel north = new JPanel();
		
		north.add(new JLabel("Port number: "+ this.server.Port));
		// to stop or start the server, we start with "Start"
		shutdownButton = new JButton("Shutdown server");
		shutdownButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent ae){
				server.shutdown();
			}
		});
		north.add(shutdownButton);

		// y despues la lista de partidas
		partidas = new JLabel(" NumPartidas: "+this.server.partidas.size());
		north.add(partidas);
//		add(north, BorderLayout.NORTH);
		clientes = new JLabel(" NumClienteS: "+this.server.clients.size());
		north.add(clientes);
		add(north, BorderLayout.NORTH);
		// y ahora saco una tabla con las partidas 
		// como estoy arrancando el servidor, no habra niunguna partida
		// id partida, num jugadores, hora inicio, hora fin
		tablemodel = new DefaultTableModel();
		tablemodel.addColumn("id partida");
		tablemodel.addColumn("num jugadores");
		tablemodel.addColumn("estado");
		tablemodel.addColumn("hora inicio");
		tablemodel.addColumn("hora fin");
		tablaPartidas = new JTable(tablemodel);
		scrollPane = new JScrollPane( tablaPartidas );
		north.add(scrollPane, BorderLayout.CENTER);
		
	}
	
	// ojo, nunca sobreescribir paintcomponents
	
	@Override
	public void paint(Graphics g) {
//		System.out.println("en paintcomponentes de razerservergui");
		super.paintComponents(g);
		partidas.setText(" NumPartidas: "+this.server.partidas.size());
		clientes.setText(" NumClienteS: "+this.server.clients.size());
		// actualizo la tabla de partidas
		if(this.server.partidas.size()!=numPartidasPrevio){
			// si ha variado el numero de partidas repinto la tabla
			for(Integer s: this.server.partidas.keySet()){
				// si no esta ya en la tabla, la pinto
				Game game = this.server.partidas.get(s);
//				System.out.println("partida con id "+game.getGameid().getClass()+":"+game.getGameid());
//				System.out.println("en la tabla: ");
//				for(int x = 0;x<tablemodel.getRowCount();x++){
//					System.out.println(tablemodel.getValueAt(x, 0).getClass()+":"+tablemodel.getValueAt(x, 0));
//				}
//				System.out.println("partidasentabla.contains(game.getGameid())?"+partidasentabla.contains(game.getGameid()));
				
				if(!partidasentabla.contains(game.getGameid())){
					tablemodel.addRow(new Object[] {game.getGameid(),
						game.getNumberOfPlayers(),
						game.getGameStatus(),
						game.getStartingtime(),
						game.getFinishingtime()} );
					partidasentabla.add(game.getGameid());
				}else{
					// si ya esta en la tabla, actualizo los valores
//					tablemodel
				}
			}
		}
		
	};

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


}
